/* eslint-disable */
import {  getUserList } from '@/api/user'
import {  getRoleList } from '@/api/role'
import {  getGroupList } from '@/api/group'
import { createMapping, createTreeData2 } from '@/utils/tools'
import { setOperatorData, getOperatorData, setOperatorMapping, getOperatorMapping } from '@/utils/storage'

const state = {
  isTried: false,
  nodeConfig: {},
  promoterDrawer: false,
  flowPermission1: {},
  approverDrawer: false,
  approverConfig1: {},
  copyerDrawer: false,
  copyerConfig1: {},
  conditionDrawer: false,
  conditionsConfig1: {
    conditionNodes: []
  },
  operatorData: getOperatorData() ||{
    user: [],
    depart: [],
    role: []
  },
  operatorMapping: getOperatorMapping() || {
    user: {},
    depart: {},
    role: {}
  },
  stationStateMapping: {
    0: {label: '审核中', type: ''},
    99: {label: '已通过', type:'success'},
    1: {label: '已通过', type:'success'},
    2: {label: '已通过', type:'success'},
    4: {label: '已通过', type:'success'},
    3: {label: '已拒绝', type: 'danger'}
  }
}

const mutations = {
  setIsTried(state, payload) {
    state.isTried = payload
  },
  setPromoter(state, payload) {
    state.promoterDrawer = payload
  },
  setFlowPermission(state, payload) {
    state.flowPermission1 = payload
  },
  setApprover(state, payload) {
    state.approverDrawer = payload
  },
  setApproverConfig(state, payload) {
    state.approverConfig1 = payload
  },
  setCopyer(state, payload) {
    state.copyerDrawer = payload
  },
  setCopyerConfig(state, payload) {
    state.copyerConfig1 = payload
  },
  setCondition(state, payload) {
    state.conditionDrawer = payload
  },
  setConditionsConfig(state, payload) {
    state.conditionsConfig1 = payload
  },
  setNodeConfig(state, payload) {
    state.nodeConfig = payload
  },
  setOperatorData(state, {data, type}) {
    state.operatorData[type] = data
    setOperatorData(state.operatorData)
  },
  setOperatorMapping(state, {data, type}) {
    state.operatorMapping[type] = data
    setOperatorMapping(state.operatorMapping)
  }
}

const actions = {
  getOperator({ commit }) {
    return new Promise((resolve) => {
      getUserList().then(res => {
        console.log("用户",res);
        res.data.forEach(item => item.operatorType = 'user')
        commit('setOperatorData', {data: res.data, type: 'user'})
        const mapping = createMapping(res.data, {keyProp: 'id', valueProp: 'name'})
        commit('setOperatorMapping', {data: mapping, type: 'user'})
      }).catch(() => {})
      getRoleList().then(res => {
        res.data.forEach(item => item.operatorType = 'role')
        commit('setOperatorData', {data: res.data, type:'role'})
        const mapping = createMapping(res.data, {keyProp: 'id', valueProp: 'name'})
        commit('setOperatorMapping', {data: mapping, type: 'role'})
      }).catch(() => {})
      // getGroupList().then(res => {
      //   res.data.forEach(item => item.operatorType = 'depart')
      //   const data = createTreeData2(res.data)
      //   commit('setOperatorData', {data, type: 'depart'})
      //   const mapping = createMapping(res.data, {keyProp: 'id', valueProp: 'name'})
      //   commit('setOperatorMapping', {data: mapping, type: 'depart'})
      // }).catch(() => {})
      resolve(true)
    })
  }
}

export default {
  namespaced: true, //命名空间
  state,
  mutations,
  actions
}
